jQuery.fn.exists = function( callback ) {
	var args = [].slice.call( arguments, 1 );
	if ( this.length ) {
		callback.call( this, args );
	}
	return this;

};

( function($) {
	var khmerScript = {
		initAll: function() {
			this.menuShowHide();
			this.slideShow();
			this.searchOpen();
		},

		menuShowHide: function() {
			var $primary_menu = $('#primary-site-navigation');
			var $secondary_menu = $('#secondary-site-navigation');

			var $first_menu = '';
			var $second_menu = '';

			if ( $primary_menu.length == 0 && $secondary_menu.length == 0 ) {
				return;
			} else {
				if ( $primary_menu.length ) {
					$first_menu = $primary_menu;
				}
			}
			var menu_wrapper = $first_menu.clone().appendTo('#smobile-menu');
			if ( $secondary_menu.length ) {
				if ( $('ul.smenu').length ) {
					var $second_menu = $secondary_menu.find('ul.smenu').clone().insertAfter('#smobile-menu .primary-menu .pmenu');
				} else {
					var $second_menu1 = $secondary_menu.find('.smenu > ul').clone().insertAfter('#smobile-menu .primary-menu .pmenu');
				}
			}
			
			$('.toggle-mobile-menu').click(function(e) {
				e.preventDefault();
				e.stopPropagation();
				
				if ( ! $('body').hasClass('mobile-menu-active') ) {
					$('#smobile-menu').show().addClass('show');
					$('body').toggleClass('mobile-menu-active');
				} else {
					khmerScript.callFunctionHideMenu();
				}
			});

			$('<span class="sub-arrow"><i class="fa fa-angle-down"></i></span>').insertAfter( $('.menu-item-has-children > a, .page_item_has_children > a') );
			$('.menu-item-has-children .sub-arrow, .page_item_has_children .sub-arrow').click(function(e) {
				e.preventDefault();
				e.stopPropagation();

				var subMenuOpen = $(this).hasClass('sub-menu-open');
				if ( subMenuOpen ) {
					$(this).removeClass('sub-menu-open');
					$(this).find('i').removeClass('fa-angle-up').addClass('fa-angle-down');
					$(this).next('ul.children, ul.sub-menu').removeClass('active').slideUp();
				} else {
					$(this).addClass('sub-menu-open');
					$(this).find('i').removeClass('fa-angle-down').addClass('fa-angle-up');
					$(this).next('ul.children, ul.sub-menu').addClass('active').slideDown();
				}
			});

			if( $('#wpadminbar').length ) {
				$('#smobile-menu').addClass('wpadminbar-active');
			}
		},

		searchOpen: function() {
			$('.btn-search').on('click', function(e) {
				e.preventDefault();
				e.stopPropagation();
				$('.search-style-one').addClass('open');
				$('.overlay').find('input').focus();
			});

			$('.overlay-close').on('click', function(e) {
				e.preventDefault();
				e.stopPropagation();
				$('.search-style-one').removeClass('open');
			});

			$(document).on('click', function(e) {
				$('.search-style-one').removeClass('open');
			});

			$('.search-style-one').click(function(e) {
				e.preventDefault();
				e.stopPropagation();
			});
		},

		slideShow: function() {
			if( $('.flexslider').length ) {
				$('.flexslider').flexslider({
					animation: "slide",
					minItems: 2,
					maxItems: 4,
					prevText: '',
					nextText: '',
					start: function(){
						$('#homepage-slide, .flex-direction-nav').show();
						$('.flexslider').css('margin-bottom', '30px');
					},
				});
			}
		},

		callFunctionHideMenu: function() {
			$('#smobile-menu').removeClass('show');
			jQuery('body').removeClass('mobile-menu-active');
			jQuery('html').removeClass('noscroll');
			jQuery('#mobile-menu-overlay').fadeOut();
		}
	};


	$( document ).ready(function(e) {
		khmerScript.initAll();
	}).on('click', function( event ) {
		khmerScript.callFunctionHideMenu();
	});
})(jQuery);

( function($) {
	// 仙尘光标
	function fairyDustCursor(isStart) {
		const possibleColors = [ '#D61C59', '#E7D84B', '#1B8798' ];
		let width = window.innerWidth;
		// eslint-disable-next-line no-unused-vars
		let height = window.innerHeight;
		const cursor = { x: width / 2, y: width / 2 };
		const particles = [];
	  
		function init() {
		  if (isStart) {
			bindEvents();
			loop();
		  } else {
			unbindEvents();
		  }
		}
	  
		function onWindowResize() {
		  width = window.innerWidth;
		  height = window.innerHeight;
		}
	  
		function onMouseMove(e) {
		  cursor.x = e.clientX;
		  cursor.y = e.clientY;
	  
		  addParticle(cursor.x, cursor.y, possibleColors[Math.floor(Math.random() * possibleColors.length)]);
		}
	  
		// Bind events that are needed
		function bindEvents() {
		  $(document).bind('mousemove', onMouseMove);
		  $(window).bind('resize', onWindowResize);
		}
	  
		function unbindEvents() {
		  $(document).unbind('mousemove');
		  $(window).unbind('resize');
		}
	  
		function addParticle(x, y, color) {
		  const particle = new Particle();
		  particle.init(x, y, color);
		  particles.push(particle);
		}
	  
		function updateParticles() {
	  
		  // Updated
		  for (let i = 0; i < particles.length; i++) {
			particles[i].update();
		  }
	  
		  // Remove dead particles
		  for (let i = particles.length - 1; i >= 0; i--) {
			if (particles[i].lifeSpan < 0) {
			  particles[i].die();
			  particles.splice(i, 1);
			}
		  }
		}
	  
		function loop() {
		  requestAnimationFrame(loop);
		  updateParticles();
		}
	  
		/**
		 * Particles
		 */
	  
		function Particle() {
	  
		  this.character = '*';
		  this.lifeSpan = 120; // ms
		  this.initialStyles = {
			position: 'fixed',
			display: 'inline-block',
			top: '0px',
			left: '0px',
			pointerEvents: 'none',
			'touch-action': 'none',
			'z-index': '10000000',
			fontSize: '25px',
			'will-change': 'transform',
		  };
	  
		  // Init, and set properties
		  this.init = function(x, y, color) {
	  
			this.velocity = {
			  x: (Math.random() < 0.5 ? -1 : 1) * (Math.random() / 2),
			  y: 1,
			};
	  
			this.position = { x: x + 10, y: y + 10 };
			this.initialStyles.color = color;
	  
			this.element = document.createElement('span');
			this.element.innerHTML = this.character;
			applyProperties(this.element, this.initialStyles);
			this.update();
	  
			document.querySelector('.js-cursor-container').appendChild(this.element);
		  };
	  
		  this.update = function() {
			this.position.x += this.velocity.x;
			this.position.y += this.velocity.y;
			this.lifeSpan--;
	  
			this.element.style.transform = 'translate3d(' + this.position.x + 'px,' + this.position.y + 'px, 0) scale(' + (this.lifeSpan / 120) + ')';
		  };
	  
		  this.die = function() {
			this.element.parentNode.removeChild(this.element);
		  };
		}
	  
		/**
		 * Utils
		 */
	  
		// Applies css `properties` to an element.
		function applyProperties(target, properties) {
		  for (const key in properties) {
			target.style[ key ] = properties[ key ];
		  }
		}
	  
		if (!('ontouchstart' in window || navigator.msMaxTouchPoints)) init();
	  }
	  
	  function setLocalCursor(value) {
		if (window.localStorage) {
		  window.localStorage.fairyDustCursor = value;
		}
	  }
	  function getLocalCursor() {
		if (window.localStorage) {
		  return window.localStorage.fairyDustCursor;
		}
		return '';
	  }
	  
	  // eslint-disable-next-line no-unused-vars
	  function fairyDustCursorToggle() {
		let isStart = getLocalCursor() === 'true';
		isStart = !isStart;
		setLocalCursor(isStart ? 'true' : 'false');
		fairyDustCursor(isStart);
	  }
	  $('#fairy-dust-cursor').click(function() {
		fairyDustCursorToggle();
	  })
	  fairyDustCursor(getLocalCursor() === 'true');
})(jQuery);
